#import "MYDynamicDetailsVC.h"

#import "MYHomeSubclassListVC.h"
#import "LMHWaterFallLayout.h"
#import "MYHomeSubclassListCollViewCell.h"
#import "MYHomeDynamicModel.h"
#import "MYDynamicDetailsVC.h"
@interface MYHomeSubclassListVC ()<UICollectionViewDelegate,UICollectionViewDataSource, LMHWaterFallLayoutDeleaget>

@property (nonatomic, weak) UICollectionView * collectionView;

/** 列数 */
@property (nonatomic, assign) NSUInteger columnCount;

@property (strong, nonatomic) NSMutableArray *homeArray;

@end

@implementation MYHomeSubclassListVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.homeArray = NSMutableArray.new;

    [self setupRefresh];
}

- (void)setupRefresh
{
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getUserHomeCollectionsApi:YES];
    }];

    self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        if (self.homeArray.count < 10) {
            [self.collectionView.mj_footer endRefreshing];
            return ;
        }
        self.page ++;

        [self getUserHomeCollectionsApi:NO];
    }];
    [self.collectionView.mj_header beginRefreshing];
    self.collectionView.mj_header.automaticallyChangeAlpha = YES;

}
#pragma mark - POST查询首页帖子列表
- (void)getUserHomeCollectionsApi:(BOOL)isReload
{
    if (isReload) {
        self.page = 1;
    }
    NSMutableDictionary *dic = NSMutableDictionary.new;

    dic[@"page"] = @(self.page);
    dic[@"limit"] = @(10);
    dic[@"channelId"] = @(self.channelId.intValue);
    NSString * token = [NSString stringWithFormat:@"%@",[YQPUserTool account].token];
    if ([token removeStringSpace].length > 10) {
        dic[@"userId"] = [YQPUserTool account].userId;
    }

    NSString *url;
    if (self.type == 0) {
        url = kPOST_INVITATION_HOME_LIST_MANAGER_URL;

    }else{
        url = kPOST_INVITATION_HOTWORDS_LIST_MANAGER_URL;

    }
    
    [YQPNetWorkingManager sendPostRequest:url parameters:dic success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        if (self.page == 1) {
            [self.homeArray removeAllObjects];
        }
        YQPTiesListModel *dynamicModel = [YQPTiesListModel mj_objectWithKeyValues:responseObject.data];
        [self.homeArray addObjectsFromArray:dynamicModel.list];
        [self.collectionView reloadData];
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshing];
        
        if (dynamicModel.list.count ==0) {
            [self.collectionView.mj_footer endRefreshingWithNoMoreData];
        }

    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshing];
    }];
}

-(void)setupView {
    [super setupView];

    for (NSInteger i=0; i<20; i++) {
        MYHomeDynamicModel *model = [[MYHomeDynamicModel alloc] init];
        if (i%3 == 0) {
            model.h = 240;
        }else if (i == 3) {
            model.h = 220;
        }else{
            model.h = 200;
        }
        model.imageName = @"Test_Rectangle_icon";
        [self.dataArr addObject:model];
    }
    [self setupLayoutAndCollectionView];
}

/**
 * 创建布局和collectionView
 */
- (void)setupLayoutAndCollectionView{
    self.columnCount = 2;
    // 创建布局
    LMHWaterFallLayout * waterFallLayout = [[LMHWaterFallLayout alloc]init];
    waterFallLayout.delegate = self;
    
    // 创建collectionView
    UICollectionView * collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-Height_TabBar-Height_NavBar-44) collectionViewLayout:waterFallLayout];
    collectionView.backgroundColor = HexColor(0xFAFAFA);
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    
    // 注册
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([MYHomeSubclassListCollViewCell class]) bundle:nil] forCellWithReuseIdentifier:@"MYHomeSubclassListCollViewCell"];
    
    self.collectionView = collectionView;
}

#pragma mark UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.homeArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MYHomeSubclassListCollViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MYHomeSubclassListCollViewCell" forIndexPath:indexPath];
    YQPTiesModel * model = nil;
    if (indexPath.item < self.homeArray.count) {
        model = self.homeArray[indexPath.item];
    }
    cell.model = model;
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    MYDynamicModel *model = self.homeArray[indexPath.row];
    MYDynamicDetailsVC *detailsVC = [[MYDynamicDetailsVC alloc] init];
    detailsVC.invitationId = model.invitationId;
    detailsVC.channelId = self.channelId;
    detailsVC.updateListBlock = ^{
        [self getUserHomeCollectionsApi:NO];
    };
    kYQPUSH(detailsVC);
}
#pragma mark UICollectionViewDataSource end

#pragma mark  - <LMHWaterFallLayoutDeleaget>
- (CGFloat)waterFallLayout:(LMHWaterFallLayout *)waterFallLayout heightForItemAtIndexPath:(NSUInteger)indexPath itemWidth:(CGFloat)itemWidth{
    
    MYHomeDynamicModel * model = self.dataArr[indexPath];
    
    return model.h+70;
}

- (CGFloat)rowMarginInWaterFallLayout:(LMHWaterFallLayout *)waterFallLayout{
    
    return 8;
    
}

- (CGFloat)columnMarginInWaterFallLayout:(LMHWaterFallLayout *)waterFallLayout {
    return 8;
}

- (NSUInteger)columnCountInWaterFallLayout:(LMHWaterFallLayout *)waterFallLayout{
    
    return 2;
    
}
#pragma mark  - <LMHWaterFallLayoutDeleaget> end
#pragma mark - JXCategoryListContentViewDelegate

- (UIView *)listView {
    return self.view;
}

- (UIScrollView *)listScrollView {
    return self.collectionView;
}
#pragma mark - JXCategoryListContentViewDelegate end
@end
